package net.hn.hnms.biz.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.quality.domain.CoalQuality;
import net.hn.hnms.biz.quality.mapper.CoalQualityMapper;
import net.hn.hnms.biz.quality.mapper.CommonMapper;
import net.hn.hnms.biz.quality.request.CoalQualityReq;
import net.hn.hnms.biz.quality.service.ICoalQualityService;
import net.hn.hnms.biz.quality.utils.CoalQualityDataListener;
import net.hn.hnms.biz.quality.vo.CoalQualityVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author ly
 * 煤质信息表服务层实现
 * @date 2022-09-09
 */

@Service
public class CoalQualityServiceImpl extends BaseService<CoalQuality, CoalQualityVO> implements ICoalQualityService {

    private CoalQualityMapper coalQualityMapper;

    public CoalQualityServiceImpl(CoalQualityMapper coalQualityMapper) {
        this.coalQualityMapper = coalQualityMapper;
    }

    /**
     * @param coalQualityReq
     * @param pageQuery   查询煤质信息列表
     */
    @Override
    public ResponsePage<CoalQualityVO> selectPageList(CoalQualityReq coalQualityReq, PageQuery pageQuery) {

        Page<CoalQuality> page = coalQualityMapper.selectPage(pageQuery.build(), buildQuery(coalQualityReq).orderByDesc(CoalQuality::getCoalQualityDate));

        return new ResponsePage<>(convert(page.getRecords(), CoalQualityVO.class, true), page.getTotal());
    }

    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<CoalQuality> 查询条件
     */
    private LambdaQueryWrapper<CoalQuality> buildQuery(CoalQualityReq req) {
        return new LambdaQueryWrapper<CoalQuality>()
                .ge(req.getQueryBeginDate() != null, CoalQuality::getCoalQualityDate, req.getQueryBeginDate())
                .le(req.getQueryEndDate() != null, CoalQuality::getCoalQualityDate, req.getQueryEndDate())
                .eq(StrUtil.isNotBlank(req.getStatus()), CoalQuality::getStatus, req.getStatus())
                .eq(StrUtil.isNotBlank(req.getMineCode()),CoalQuality::getMineCode, req.getMineCode());
    }

    /**
     * @param CoalQuality 查询煤质信息列表
     */
    @Override
    public List<CoalQualityVO> selectList(CoalQualityReq CoalQuality) {
        List<CoalQuality> list = coalQualityMapper.selectList(buildQuery(CoalQuality));
        return convert(list, CoalQualityVO.class, false);
    }

    /**
     * @param req 新增煤质信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CoalQualityReq req) {

        CoalQuality insertObj = new CoalQuality();
        BeanUtil.copyProperties(req, insertObj);
        insertObj.setStatus("0");
        QueryWrapper<CoalQuality> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coal_quality_date", insertObj.getCoalQualityDate());
        if (coalQualityMapper.selectCount(queryWrapper) > 0) {
            throw new HuaNengException(Constants.FAIL, "已存在同一时段的煤质数据");
        }
        return coalQualityMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除 id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        coalQualityMapper.deleteBatchIds(idList);
    }

    /**
     * @param req 单个修改 按照主键修改信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(CoalQualityReq req) {
        if (StrUtil.isBlank(req.getCoalQualityId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        CoalQuality updateObj = new CoalQuality();
        BeanUtil.copyProperties(req, updateObj);
        QueryWrapper<CoalQuality> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coal_quality_date", updateObj.getCoalQualityDate());
        queryWrapper.ne("coal_quality_id", updateObj.getCoalQualityId());
        if (coalQualityMapper.selectCount(queryWrapper) > 0) {
            throw new HuaNengException(Constants.FAIL, "已存在同一时段的煤质数据");
        }
        int updateCount = coalQualityMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    /**
     * @param id 按主键id查询详情
     */
    @Override
    public CoalQualityVO selectDetailById(String id) {
        CoalQuality coalQuality = coalQualityMapper.selectById(id);
        if (coalQuality == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(coalQuality, CoalQualityVO.class);
    }


    /**
     * 启用/禁用
     *
     * @param id     数据id
     * @param status status: 0:启用 1:禁用
     */
    public void changeStatus(String id, Integer status) {
        if (status == 1 || status == 0) {
            CoalQuality planMining = coalQualityMapper.selectById(id);
            if (planMining == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            planMining.setStatus(status + "");
            int res = coalQualityMapper.updateById(planMining);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }

            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");
    }

    @Override
    public void importExcel(Long date, MultipartFile file, CommonMapper commonMapper) {

        String sufix = StrUtil.subAfter(file.getOriginalFilename(), ".", true);
        if (!"xls".equals(sufix) && !"xlsx".equals(sufix))
            throw new HuaNengException(Constants.FAIL, "文件格式仅支持xls或xlsx");

        try {
            EasyExcel.read(file.getInputStream(), CoalQuality.class, new CoalQualityDataListener(date,commonMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new HuaNengException(Constants.FAIL, "内容格式错误！");
        }

    }

    @Override
    public List<CoalQualityVO> getCoalAnalysisMonthlyRollup(String startDate) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        PageQuery p = new PageQuery();
        p.setPageNum(0);
        p.setPageSize(99999);

//        Map<String, List<CoalQualityVO>> map = new HashMap<>();
        List<String> dataList=getMonthByYear(Integer.parseInt(startDate));
        List<CoalQualityVO> cList = new ArrayList<>();

        for (String fData : dataList) {


            List<String> list = new ArrayList<>();
            list.add(fData);
            list.add(fData);
            CoalQualityReq coalQualityReq = new CoalQualityReq();
            coalQualityReq.setDateTimeRange(list);
            ResponsePage<CoalQualityVO> coalQualityVOResponsePage = selectPageList(coalQualityReq, p);
            BigDecimal salesVolumeCount = new BigDecimal(0);
            BigDecimal homeSaleCount = new BigDecimal(0);
            BigDecimal exportSaleCount = new BigDecimal(0);
            if(coalQualityVOResponsePage.getPageData()==null||coalQualityVOResponsePage.getPageData().size()==0){
                break;
            }
            for (CoalQualityVO c : coalQualityVOResponsePage.getPageData()) {
                if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("0"))homeSaleCount = homeSaleCount.add(c.getSalesVolume());
                if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("1"))exportSaleCount = exportSaleCount.add(c.getSalesVolume());
                salesVolumeCount = salesVolumeCount.add(c.getSalesVolume());
            }
            CoalQualityVO homeSaleVo = getNullCoal();
            CoalQualityVO exportSaleVo = getNullCoal();
            CoalQualityVO countDataVo=getNullCoal();
            homeSaleVo.setSellWay("0");
            exportSaleVo.setSellWay("1");
            homeSaleVo.setSalesVolume(homeSaleCount);
            exportSaleVo.setSalesVolume(exportSaleCount);
            try {
//                homeSaleVo.setCoalQualityDate(sdf.parse(fData));
//                exportSaleVo.setCoalQualityDate(sdf.parse(fData));
//                countDataVo.setCoalQualityDate(sdf.parse(fData));
                homeSaleVo.setCountDate(sdf.parse(fData));
                exportSaleVo.setCountDate(sdf.parse(fData));
                countDataVo.setCountDate(sdf.parse(fData));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

            coalQualityVOResponsePage.getPageData().forEach(c -> {
                if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("0")) {
                    homeSaleVo.setMoistureTotal(homeSaleVo.getMoistureTotal().add(c.getMoistureTotal().multiply(c.getSalesVolume())));
                    homeSaleVo.setAsh(homeSaleVo.getAsh().add(c.getAsh().multiply(c.getSalesVolume())));
                    homeSaleVo.setVolatileMatter(homeSaleVo.getVolatileMatter().add(c.getVolatileMatter().multiply(c.getSalesVolume())));
                    homeSaleVo.setShaleContent(homeSaleVo.getShaleContent().add(c.getShaleContent().multiply(c.getSalesVolume())));
                    homeSaleVo.setNetCalorificValue(homeSaleVo.getNetCalorificValue().add(c.getNetCalorificValue().multiply(c.getSalesVolume())));
                }
                if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("1")) {
                    exportSaleVo.setMoistureTotal(exportSaleVo.getMoistureTotal().add(c.getMoistureTotal().multiply(c.getSalesVolume())));
                    exportSaleVo.setAsh(exportSaleVo.getAsh().add(c.getAsh().multiply(c.getSalesVolume())));
                    exportSaleVo.setVolatileMatter(exportSaleVo.getVolatileMatter().add(c.getVolatileMatter().multiply(c.getSalesVolume())));
                    exportSaleVo.setShaleContent(exportSaleVo.getShaleContent().add(c.getShaleContent().multiply(c.getSalesVolume())));
                    exportSaleVo.setNetCalorificValue(exportSaleVo.getNetCalorificValue().add(c.getNetCalorificValue().multiply(c.getSalesVolume())));
                }
            });
            if (homeSaleCount.compareTo(new BigDecimal(0)) == 1) {
                //全水分
                homeSaleVo.setMoistureTotal(homeSaleVo.getMoistureTotal().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                //灰分
                homeSaleVo.setAsh(homeSaleVo.getAsh().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                //挥发份
                homeSaleVo.setVolatileMatter(homeSaleVo.getVolatileMatter().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                //全硫分
                homeSaleVo.setShaleContent(homeSaleVo.getShaleContent().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                //发热量
                homeSaleVo.setNetCalorificValue(homeSaleVo.getNetCalorificValue().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));

            }
            if (exportSaleCount.compareTo(new BigDecimal(0)) == 1) {
                exportSaleVo.setMoistureTotal(exportSaleVo.getMoistureTotal().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                exportSaleVo.setAsh(exportSaleVo.getAsh().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                exportSaleVo.setVolatileMatter(exportSaleVo.getVolatileMatter().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                exportSaleVo.setShaleContent(exportSaleVo.getShaleContent().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
                exportSaleVo.setNetCalorificValue(exportSaleVo.getNetCalorificValue().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            }

            cList.add(homeSaleVo);
            cList.add(exportSaleVo);


            Calendar calendar = Calendar.getInstance();
            Date date = null;
            try {
                date = sdf.parse(fData);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            calendar.setTime(date);
            int MonthDays=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            CoalQualityVO countHomeVo=getNullCoal();
            CoalQualityVO countExportVo=getNullCoal();
            BigDecimal HomeExportSum = new BigDecimal(0);
            BigDecimal sumMt = new BigDecimal(0);
            BigDecimal sumAsh = new BigDecimal(0);
            BigDecimal sumVm = new BigDecimal(0);
            BigDecimal sumSc = new BigDecimal(0);
            BigDecimal sumNcv = new BigDecimal(0);
            for (int i = 1; i <= MonthDays; i++) {
                boolean isHome=false;
                boolean isExport=false;
                String newDate = fData + "-" + (i < 10 ? "0" + i : i);
                if(coalQualityVOResponsePage.getPageData()==null||coalQualityVOResponsePage.getPageData().size()==0){
                    break;
                }
                for (CoalQualityVO c : coalQualityVOResponsePage.getPageData()) {
                    String souceDate = new SimpleDateFormat("YYYY-MM-dd").format(c.getCoalQualityDate());
                    if (souceDate.equals(newDate)) {
                        String s = c.getSellWay() == "0" ? "" : "";
                        if (c.getSellWay().equals("0")) {
                            countHomeVo = c;
                            isHome=true;
                        }
                        if (c.getSellWay().equals("1")) {
                            countExportVo = c;
                            isExport=true;
                        }

                    }

                }
                if(!isHome) countHomeVo = getNullCoal();
                if(!isExport) countExportVo = getNullCoal();
                //每天合计销量
                HomeExportSum = countHomeVo.getSalesVolume().add(countExportVo.getSalesVolume());
                if(HomeExportSum.compareTo(new BigDecimal(0))==0){
                    break;
                }
                //内销全水分
                BigDecimal n1=countHomeVo.getSalesVolume().multiply(countHomeVo.getMoistureTotal());
                //外销全水分
                BigDecimal w1=countExportVo.getSalesVolume().multiply(countExportVo.getMoistureTotal());
                //每天全水分
                BigDecimal mt = countDataVo.getMoistureTotal().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getMoistureTotal()).add(countExportVo.getSalesVolume().multiply(countExportVo.getMoistureTotal())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天全水分 * 每天合计销量
                BigDecimal countMt = mt.multiply(HomeExportSum);
                //计算月合计全水分总和， 等待 / 月总销量  HomeExportSum
                sumMt = sumMt.add(countMt);

                //每天灰分
                BigDecimal ash = countDataVo.getAsh().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getAsh()).add(countExportVo.getSalesVolume().multiply(countExportVo.getAsh())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天灰分 * 每天合计销量
                BigDecimal countAsh = ash.multiply(HomeExportSum);
                //计算月合计灰分总和， 等待 / 月总销量  HomeExportSum
                sumAsh = sumAsh.add(countAsh);

                //每天挥发份
                BigDecimal vm = countDataVo.getVolatileMatter().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getVolatileMatter()).add(countExportVo.getSalesVolume().multiply(countExportVo.getVolatileMatter())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天挥发份 * 每天合计销量
                BigDecimal countVm = vm.multiply(HomeExportSum);
                //计算月合计挥发份总和， 等待 / 月总销量  HomeExportSum
                sumVm = sumVm.add(countVm);

                //每天全硫分
                BigDecimal sc = countDataVo.getShaleContent().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getShaleContent()).add(countExportVo.getSalesVolume().multiply(countExportVo.getShaleContent())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天全硫分 * 每天合计销量
                BigDecimal countSc = sc.multiply(HomeExportSum);
                //计算月合计全硫分总和， 等待 / 月总销量  HomeExportSum
                sumSc = sumSc.add(countSc);

                //每天发热量
                BigDecimal ncv = countDataVo.getNetCalorificValue().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getNetCalorificValue()).add(countExportVo.getSalesVolume().multiply(countExportVo.getNetCalorificValue())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天发热量 * 每天合计销量
                BigDecimal countNcv = ncv.multiply(HomeExportSum);
                //计算月合计发热量总和， 等待 / 月总销量  HomeExportSum
                sumNcv = sumNcv.add(countNcv);



//                if (i == MonthDays) {
//                    countDataVo.setMoistureTotal(sumMt.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
//                    countDataVo.setAsh(sumAsh.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
//                    countDataVo.setVolatileMatter(sumVm.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
//                    countDataVo.setShaleContent(sumSc.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
//                    countDataVo.setNetCalorificValue(sumNcv.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
//                    countDataVo.setSalesVolume(salesVolumeCount);
//                    countDataVo.setSellWay("3");
//                    cList.add(countDataVo);
//                }



            }
            countDataVo.setMoistureTotal(sumMt.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
            countDataVo.setAsh(sumAsh.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
            countDataVo.setVolatileMatter(sumVm.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
            countDataVo.setShaleContent(sumSc.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
            countDataVo.setNetCalorificValue(sumNcv.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
            countDataVo.setSalesVolume(salesVolumeCount);
            countDataVo.setSellWay("3");
            cList.add(countDataVo);
//            map.put(fData, cList);

        }


        return cList;
    }


    public CoalQualityVO getNullCoal() {
        CoalQualityVO v = new CoalQualityVO();
        v.setMoistureTotal(new BigDecimal(0));
        v.setAsh(new BigDecimal(0));
        v.setNetCalorificValue(new BigDecimal(0));
        v.setSalesVolume(new BigDecimal(0));
        v.setShaleContent(new BigDecimal(0));
        v.setVolatileMatter(new BigDecimal(0));
        return v;
    }


    @Override
    public List<CoalQualityVO> getCoalAnalysisYearRollup(String year) {
        List<CoalQualityVO> cList = new ArrayList<>();
        List<String> dataList=getDateListByYear(Integer.parseInt(year));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        PageQuery p = new PageQuery();
        p.setPageNum(0);
        p.setPageSize(99999);
        List<String> list = new ArrayList<>();
        list.add(year+"-01-01");
        list.add(year+"-12-31");
        CoalQualityReq coalQualityReq = new CoalQualityReq();
        coalQualityReq.setDateTimeRange(list);
        ResponsePage<CoalQualityVO> coalQualityVOResponsePage = selectPageList(coalQualityReq, p);
        BigDecimal salesVolumeCount = new BigDecimal(0);
        BigDecimal homeSaleCount = new BigDecimal(0);
        BigDecimal exportSaleCount = new BigDecimal(0);
        for (CoalQualityVO c : coalQualityVOResponsePage.getPageData()) {
            if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("0"))homeSaleCount = homeSaleCount.add(c.getSalesVolume());
            if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("1"))exportSaleCount = exportSaleCount.add(c.getSalesVolume());
            salesVolumeCount = salesVolumeCount.add(c.getSalesVolume());
        }
        CoalQualityVO homeSaleVo = getNullCoal();
        CoalQualityVO exportSaleVo = getNullCoal();
        CoalQualityVO countDataVo=getNullCoal();
        homeSaleVo.setSellWay("0");
        exportSaleVo.setSellWay("1");
        homeSaleVo.setSalesVolume(homeSaleCount);
        exportSaleVo.setSalesVolume(exportSaleCount);

        coalQualityVOResponsePage.getPageData().forEach(c -> {
            if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("0")) {
                homeSaleVo.setMoistureTotal(homeSaleVo.getMoistureTotal().add(c.getMoistureTotal().multiply(c.getSalesVolume())));
                homeSaleVo.setAsh(homeSaleVo.getAsh().add(c.getAsh().multiply(c.getSalesVolume())));
                homeSaleVo.setVolatileMatter(homeSaleVo.getVolatileMatter().add(c.getVolatileMatter().multiply(c.getSalesVolume())));
                homeSaleVo.setShaleContent(homeSaleVo.getShaleContent().add(c.getShaleContent().multiply(c.getSalesVolume())));
                homeSaleVo.setNetCalorificValue(homeSaleVo.getNetCalorificValue().add(c.getNetCalorificValue().multiply(c.getSalesVolume())));
            }
            if (!StringUtils.isBlank(c.getSellWay()) && c.getSellWay().equals("1")) {
                exportSaleVo.setMoistureTotal(exportSaleVo.getMoistureTotal().add(c.getMoistureTotal().multiply(c.getSalesVolume())));
                exportSaleVo.setAsh(exportSaleVo.getAsh().add(c.getAsh().multiply(c.getSalesVolume())));
                exportSaleVo.setVolatileMatter(exportSaleVo.getVolatileMatter().add(c.getVolatileMatter().multiply(c.getSalesVolume())));
                exportSaleVo.setShaleContent(exportSaleVo.getShaleContent().add(c.getShaleContent().multiply(c.getSalesVolume())));
                exportSaleVo.setNetCalorificValue(exportSaleVo.getNetCalorificValue().add(c.getNetCalorificValue().multiply(c.getSalesVolume())));
            }
        });
        if (homeSaleCount.compareTo(new BigDecimal(0)) == 1) {
            //全水分
            homeSaleVo.setMoistureTotal(homeSaleVo.getMoistureTotal().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            //灰分
            homeSaleVo.setAsh(homeSaleVo.getAsh().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            //挥发份
            homeSaleVo.setVolatileMatter(homeSaleVo.getVolatileMatter().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            //全硫分
            homeSaleVo.setShaleContent(homeSaleVo.getShaleContent().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            //发热量
            homeSaleVo.setNetCalorificValue(homeSaleVo.getNetCalorificValue().divide(homeSaleCount, 2, BigDecimal.ROUND_HALF_UP));

        }
        if (exportSaleCount.compareTo(new BigDecimal(0)) == 1) {
            exportSaleVo.setMoistureTotal(exportSaleVo.getMoistureTotal().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            exportSaleVo.setAsh(exportSaleVo.getAsh().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            exportSaleVo.setVolatileMatter(exportSaleVo.getVolatileMatter().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            exportSaleVo.setShaleContent(exportSaleVo.getShaleContent().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
            exportSaleVo.setNetCalorificValue(exportSaleVo.getNetCalorificValue().divide(exportSaleCount, 2, BigDecimal.ROUND_HALF_UP));
        }

        cList.add(homeSaleVo);
        cList.add(exportSaleVo);

        CoalQualityVO countHomeVo=getNullCoal();
        CoalQualityVO countExportVo=getNullCoal();
        BigDecimal HomeExportSum = new BigDecimal(0);
        BigDecimal sumMt = new BigDecimal(0);
        BigDecimal sumAsh = new BigDecimal(0);
        BigDecimal sumVm = new BigDecimal(0);
        BigDecimal sumSc = new BigDecimal(0);
        BigDecimal sumNcv = new BigDecimal(0);

        int index=0;
        for (String s : dataList) {
            index++;
            boolean isHome=false;
            boolean isExport=false;
            String newDate = s;
            for (CoalQualityVO c : coalQualityVOResponsePage.getPageData()) {
                String souceDate = new SimpleDateFormat("YYYY-MM-dd").format(c.getCoalQualityDate());
                if (souceDate.equals(newDate)) {
                    if (c.getSellWay().equals("0")) {
                        countHomeVo = c;
                        isHome=true;
                    }
                    if (c.getSellWay().equals("1")) {
                        countExportVo = c;
                        isExport=true;
                    }

                }

            }
            if(!isHome) countHomeVo = getNullCoal();
            if(!isExport) countExportVo = getNullCoal();
            if(isExport||isHome){

                //每天合计销量
                HomeExportSum = countHomeVo.getSalesVolume().add(countExportVo.getSalesVolume());
                //内销全水分
                BigDecimal n1=countHomeVo.getSalesVolume().multiply(countHomeVo.getMoistureTotal());
                //外销全水分
                BigDecimal w1=countExportVo.getSalesVolume().multiply(countExportVo.getMoistureTotal());
                //每天全水分
                BigDecimal mt = countDataVo.getMoistureTotal().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getMoistureTotal()).add(countExportVo.getSalesVolume().multiply(countExportVo.getMoistureTotal())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天全水分 * 每天合计销量
                BigDecimal countMt = mt.multiply(HomeExportSum);
                //计算月合计全水分总和， 等待 / 月总销量  HomeExportSum
                sumMt = sumMt.add(countMt);

                //每天灰分
                BigDecimal ash = countDataVo.getAsh().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getAsh()).add(countExportVo.getSalesVolume().multiply(countExportVo.getAsh())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天灰分 * 每天合计销量
                BigDecimal countAsh = ash.multiply(HomeExportSum);
                //计算月合计灰分总和， 等待 / 月总销量  HomeExportSum
                sumAsh = sumAsh.add(countAsh);

                //每天挥发份
                BigDecimal vm = countDataVo.getVolatileMatter().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getVolatileMatter()).add(countExportVo.getSalesVolume().multiply(countExportVo.getVolatileMatter())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天挥发份 * 每天合计销量
                BigDecimal countVm = vm.multiply(HomeExportSum);
                //计算月合计挥发份总和， 等待 / 月总销量  HomeExportSum
                sumVm = sumVm.add(countVm);

                //每天全硫分
                BigDecimal sc = countDataVo.getShaleContent().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getShaleContent()).add(countExportVo.getSalesVolume().multiply(countExportVo.getShaleContent())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天全硫分 * 每天合计销量
                BigDecimal countSc = sc.multiply(HomeExportSum);
                //计算月合计全硫分总和， 等待 / 月总销量  HomeExportSum
                sumSc = sumSc.add(countSc);

                //每天发热量
                BigDecimal ncv = countDataVo.getNetCalorificValue().add(countHomeVo.getSalesVolume().multiply(countHomeVo.getNetCalorificValue()).add(countExportVo.getSalesVolume().multiply(countExportVo.getNetCalorificValue())).divide(HomeExportSum, 11, BigDecimal.ROUND_HALF_UP));
                //每天发热量 * 每天合计销量
                BigDecimal countNcv = ncv.multiply(HomeExportSum);
                //计算月合计发热量总和， 等待 / 月总销量  HomeExportSum
                sumNcv = sumNcv.add(countNcv);
            }




            if (index == dataList.size()) {
                countDataVo.setMoistureTotal(sumMt.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
                countDataVo.setAsh(sumAsh.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
                countDataVo.setVolatileMatter(sumVm.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
                countDataVo.setShaleContent(sumSc.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
                countDataVo.setNetCalorificValue(sumNcv.divide(salesVolumeCount, 2, BigDecimal.ROUND_HALF_UP));
                countDataVo.setSalesVolume(salesVolumeCount);
                countDataVo.setSellWay("3");
                cList.add(countDataVo);
            }

        }


//        for (int i = 1; i <= dataList.size(); i++) {
//
//
//        }
        return cList;
    }



    private static List<String> getDateListByYear(Integer year){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<String> allDateList = new ArrayList<>();
        LocalDate startDate = LocalDate.of(year, 1, 01);
        LocalDate endDate = startDate;
        while (year == endDate.getYear()) {
            allDateList.add(endDate.format(formatter));
            endDate = endDate.plusDays(1L);
        }
        return allDateList;
    }

    public static List<String> getMonthByYear(int year){
        List<String> data = new ArrayList<>();
        try {
            Calendar c = Calendar.getInstance();
            // 获取当前的年份
//            int year = c.get(Calendar.YEAR);
            // 定义时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            // 开始日期为当前年拼接1月份
            Date startDate = sdf.parse(year + "-01");
            // 结束日期为当前年拼接12月份
            Date endDate = sdf.parse(year + "-12");
            // 设置calendar的开始日期
            c.setTime(startDate);
            // 当前时间小于等于设定的结束时间
            while(c.getTime().compareTo(endDate) <= 0){
                String time = sdf.format(c.getTime());
                data.add(time);
                // 当前月份加1
                c.add(Calendar.MONTH, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return data;
    }
}
